package com.hszh.common.controller.packge;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hszh.common.config.ApiContext;
import com.hszh.common.dto.packge.DropdownListBoxTypeDto;
import com.hszh.common.service.packge.WmsBoxTypeService;
import com.hszh.common.service.packge.WmsBoxTypeServiceEx;
import com.farvision.common.api.CommonResult;
import com.hszh.wms.mbg.packge.entity.WmsBoxType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.farvision.common.api.CommonResult.success;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author niufw
 * @since 2021-02-27
 */
@Api(tags = "WmsBoxTypeController", description = "装箱管理")
@Controller
@RequestMapping("/boxType")
public class WmsBoxTypeController {
    private static final Logger LOGGER = LoggerFactory.getLogger(WmsBoxTypeController.class);

    //多代码版本
    //@Autowired
    //private IWmsBoxTypeService demoService;
    //简化代码版本
    @Autowired
    private WmsBoxTypeService demoService;
    @Autowired
    private WmsBoxTypeServiceEx demoService2;
    @Autowired
    private ApiContext apiContext;

    private boolean logined;

//    @ApiOperation("获取所有装箱类型列表")
//    @RequestMapping(value = "listAll", method = RequestMethod.GET)
//    @ResponseBody
//    public List<WmsBoxType> getboxTypeList2()  {
//        //单个捕获
//        try {
//            int i = 1/0;
//        // apiContext.setCurrentTenantId(2L);
//             return null;
//        }
//        catch (Exception ex)
//        {
//            //第一种方式,抛出异常
//           // throw ex;
//            //记录日志
//            LOGGER.error(ex.getMessage());
//            //return failed("运行时错误");
//        }
//    }

    @ApiOperation("获取所有装箱类型列表")
    @RequestMapping(value = "listAll", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<WmsBoxType>> getboxTypeList()  {
        //单个捕获
        //try {
//        if(!logined)
//        {
//            Asserts.fail("认证错误！");
//        }
           int i = 1/0;
           // apiContext.setCurrentTenantId(2L);
            return  success(demoService.list(null));
//        }
//        catch (Exception ex)
//        {
//            //第一种方式,抛出异常
//           // throw ex;
//            //记录日志
//            LOGGER.error(ex.getMessage());
//            return failed("运行时错误");
//        }
    }

    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult createboxType(@RequestBody WmsBoxType WmsBoxType) {
        CommonResult commonResult;
        //v1
        //int count = demoService.insert(WmsBoxType);
        //v2
        int count = demoService.save(WmsBoxType)? 1 : 0;
        if (count == 1) {
            commonResult = success(WmsBoxType);
            //LOGGER.debug("createboxType success:{}", WmsBoxType);
        } else {
            commonResult = CommonResult.failed("操作失败");
            //LOGGER.debug("createboxType failed:{}", WmsBoxType);
        }
        return commonResult;
    }

    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult updateboxType(@PathVariable("id") Long id, @RequestBody WmsBoxType WmsBoxTypeDto, BindingResult result) {
        CommonResult commonResult;
        //v1
        //int count = demoService.updateById(id, WmsBoxTypeDto);
        //v2
        int count = demoService.updateById(WmsBoxTypeDto)?1:0;
        if (count == 1) {
            commonResult = success(WmsBoxTypeDto);
            //LOGGER.debug("updateboxType success:{}", WmsBoxTypeDto);
        } else {
            commonResult = CommonResult.failed("操作失败");
            //LOGGER.debug("updateboxType failed:{}", WmsBoxTypeDto);
        }
        return commonResult;
    }

    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult deleteboxType(@PathVariable("id") Long id) {
        //v1
        //int count = demoService.deleteById(id);
        //v2
        int count = demoService.removeById(id)?1:0;

        if (count == 1) {
            //LOGGER.debug("deleteboxType success :id={}", id);
            return success(null);
        } else {
            //LOGGER.debug("deleteboxType failed :id={}", id);
            return CommonResult.failed("操作失败");
        }
    }

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<IPage<WmsBoxType>> listboxType(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                      @RequestParam(value = "pageSize", defaultValue = "3") Integer pageSize) {
        apiContext.setCurrentTenantId(1L);

        IPage<WmsBoxType> page = new Page<>(1, 5);
        IPage<WmsBoxType> boxTypeList = demoService.page(page,new QueryWrapper<WmsBoxType>().orderByDesc("id"));
        return success(boxTypeList);
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<WmsBoxType> boxType(@PathVariable("id") Long id) {
        return success(demoService.getById(id));
    }

    //xml 自定义sql demo
    @RequestMapping(value = "/basic", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<DropdownListBoxTypeDto>> getDLData(@RequestParam(value = "tenantid") Long tenantid) {
        //sql版本
        //从rquest中获取tenantid
        apiContext.setCurrentTenantId(tenantid);
        return success(demoService2.selectByNoName());

        //v2 存储过程版本
        //return success(demoService2.selectByNoName2());
    }

}
